using System;

namespace csammisrun.OscarLib.Utility
{
	/// <summary>
	/// Provides static methods for processing messages in SNAC family 0x0018 -- e-mail notification
	/// </summary>
	internal static class SNAC18
	{
    /// <summary>
		/// Processes a notification of e-mail information change -- SNAC(18,07)
		/// </summary>
		/// <param name="dp">A <see cref="DataPacket"/> object containing SNAC(18,07)</param>
		public static void ProcessEmailInformation(DataPacket dp)
		{
			byte[] SNACData = dp.Data;

			int index = 0;
			ushort key, keylength;

			int num_messages = -1;
			ushort flags;
			byte unread = 0;
			string url = "", alerturl = "", alerttitle = "", domain = "";

			// There are 24 bytes of cookies prior to the TLV list, an 8-byte cookie
			// and a 16-byte cookie. The 16 byte cookie is identifying information
			// that may be useful
			index += 8;
			byte[] idcookie = new byte[16];
			Marshal.CopyArray(SNACData, idcookie, ref index);

			while(index < SNACData.Length)
			{
				key = Marshal.ByteArrayToUshort(SNACData, ref index);
				keylength = Marshal.ByteArrayToUshort(SNACData, ref index);
				switch(key)
				{
					case 0x0005:
						alerttitle = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x0007:
            url = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x000D:
            alerturl = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x0080:
						num_messages = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					case 0x0081:
						unread = SNACData[index++];
						if(unread == 0)
						{
							num_messages = 0;
						}
						break;
					case 0x0082:
            domain = Marshal.ByteArrayToString(SNACData, ref index, keylength, Marshal.ASCII);
						break;
					case 0x0084:
						flags = Marshal.ByteArrayToUshort(SNACData, ref index);
						break;
					default:
						index += keylength;
						break;
				}
			}

			if(num_messages == -1)
			{
				num_messages = 1;
			}
		}
	}
}
